/**
 * Formcheck
 *
 * Binds function to the submit event of a form and checks all
 * fields that has the class 'checkme' (or the user's given one).
 * 
 * Can check for:
 * - filled in
 * - E-mail
 * - Postcode (1234AA or 1234 AA)
 * - Date of birth ( (d)d/(m)m/yyyy )
 * 
 * Type of field can be defined in the rel attritute.
 * e.g. <input type="text" name="email" rel="email" class="checkme" />
 *
 * @subpackage	jQuery
 * @author		Koos van Egmond | 7U Digital Handmade Originals
 */

(function($) {
	
	$.fn.formcheck = function(opts) {
		
		/**
		 * Default options
		 */
		var defaults = {
			classname		: 'checkme',
			onSuccess		: function() {},
			onError			: function() {},
			onFieldError	: function(el) {},
			beforeCheck		: function() {}
		};
		
		/**
		 * Merge the user's options with the defaults
		 */
		opts = $.extend(defaults, opts);
		
		/**
		 * Set the current function in another scope for compatibility
		 */
		var fc = this;
		
		/**
		 * Bind submit event to the current form and do correct
		 * actions.
		 */
		return this.submit(function()
		{
			// Start by defining the state
			var state = true;
			
			// Start executing the beforeCheck function
			if (typeof opts.beforeCheck == 'function')
				opts.beforeCheck();
			
			// Loop through all fields that has the 'checkme' class
			$('.'+opts.classname).each(function()
			{
				// Set alias
				var t = $(this);
				
				// Get the rel attr for value type
				var type = t.attr('rel') || 'none';
				
				// Check for a rel
				switch (type.toLowerCase())
				{
					// E-MAIL
					case 'email':
						var s = checkEmail( t.val() );
					break;
					
					// POSTCODE
					case 'postcode':
						var s = checkPostcode( t.val() );
					break;
					
					// DATE OF BIRTH
					case 'dob':
					case 'dateofbirth':
						var s = checkDOB( t.val() );
					break;
					
					// NO TYPE, JUST EMPTY CHECK
					default:
						var s = (t.val() != '');
					break;
				}
				
				// var 's' must be true in order to pass
				if (!s) {
					// Set state to false
					state = false;
					
					// Execute a field error function?
					if (typeof opts.onFieldError == 'function')
						opts.onFieldError(t);
				}
			});
			
			// Execute the onError function ?
			if (!state && typeof opts.onError == 'function')
				opts.onError();
			
			// Execute the onSuccess function if one is defined
			if (state && typeof opts.onSuccess == 'function')
				return opts.onSuccess();
			
			return state;
		});
		
		/**
		 * Function to check an E-mail address
		 */
		function checkEmail(val)
		{
			// Set regex for checking email
			var r = /[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i;
			
			// Do the check
			return r.test(val);
		}
		
		/**
		 * Function to check a date of birth in dd/mm/yyyy format
		 */
		function checkDOB(val)
		{
			// Set regex for checking date of birth
			var r = /\d{1,2}\/\d{1,2}\/\d{4}/i;
			
			// Do the check
			return r.test(val);
		}
		
		/**
		 * Function to check a postcode in current formats, where
		 * n is number and c is character:
		 * * nnnncc
		 * * nnnn cc
		 */
		function checkPostcode(val)
		{
			// Set regex for checking postcodes
			var r = /\d{4}.?[a-zA-Z]{2}/i;
			
			// Do the check
			return r.test(val);
		}
		
	};
	
})(jQuery);